import time
from enum import IntEnum
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

#查找元素的类型
class find_type(IntEnum):
    css_type = 1
    xpath_type = 2

class Test_02 :
    def __init__(self,path):
        self.service = Service(path)
        self.driver = webdriver.Chrome(service=self.service)
    def __del__(self):
        self.driver.quit()
    def get(self,url):
        self.driver.get(url)
    def find_by_select(self,val):
        return self.driver.find_element(By.CSS_SELECTOR,val)
    def finds_by_select(self,val):
        ret = self.driver.find_elements(By.CSS_SELECTOR, val)
        return ret
    def find_by_xpath(self,path):
        return self.driver.find_element(By.XPATH,path)
    def finds_by_xpath(self,path):
        return self.driver.find_elements(By.XPATH,path)
    #发起点击事件
    def click(self, Type, val):
        #先找到指定元素
        if Type == find_type.css_type:
            self.find_by_select(val).click()
        else:
            self.find_by_xpath(val).click()
    #模拟按键输入
    def emulate_key_inputs(self,Type,val,text):
        if Type == find_type.css_type:
            self.find_by_select(val).send_keys(text)
        else:
            self.find_by_xpath(val).send_keys(text)
    #清除按键键入的数据
    def clear(self,Type,val):
        if Type == find_type.css_type:
            self.find_by_select(val).clear()
        else:
            self.find_by_xpath(val).clear()
    #获取文本信息,注意不是属性信息
    def get_text_from_element(self,Type,val):
        if Type == find_type.css_type:
            return self.find_by_select(val).text
        else:
            return self.find_by_xpath(val).text



def test1():
    t1 = Test_02('C:\driver\chromedriver-win64\chromedriver.exe')
    t1.get("https://www.baidu.com")
    t1.find_by_select("#kw")
    ret = t1.finds_by_select("#hotsearch-content-wrapper > li")
    for i in ret:
        print(i.text)
def test2():
    t1 = Test_02('C:\driver\chromedriver-win64\chromedriver.exe')
    print("开始使用xpath方式")
    ret = t1.finds_by_xpath('//*[@id="hotsearch-content-wrapper"]')
    for i in ret:
        print(i.text)
def test3():
    t1 = Test_02('C:\driver\chromedriver-win64\chromedriver.exe')
    t1.get("https://www.baidu.com")
    print("开始使用css+点击方式")
    t1.click(find_type.css_type,"#su")
    time.sleep(2)
def test4():
    t1 = Test_02('C:\driver\chromedriver-win64\chromedriver.exe')
    t1.get("https://www.baidu.com")
    print("开始使用xpath+点击方式")
    t1.click(find_type.xpath_type,'//*[@id="su"]')
    time.sleep(2)
def test5():
    t1 = Test_02('C:\driver\chromedriver-win64\chromedriver.exe')
    t1.get("https://www.baidu.com")
    print("开始使用模拟按键输入")
    t1.emulate_key_inputs(find_type.css_type,"#kw","斗破苍穹")
    time.sleep(2)
    t1.clear(find_type.css_type,"#kw")
    time.sleep(2)
    print("开始使用获取文本信息")
    t1.emulate_key_inputs(find_type.css_type, "#kw", "斗破苍穹")
    time.sleep(2)
    text = t1.get_text_from_element(find_type.css_type,"#kw")
    info = "获取到的文本:{body}".format(body = text)
    print(info)
def main() :
    #test1() 测试css选择器
    #test2() 测试xpath选择器
    # test3()
    # test4()
    #test5()

main()